package com.lv.common.dialog

import android.content.Context
import android.text.*
import android.text.method.DigitsKeyListener
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.lv.common.R
import com.lv.common.base.BaseDialog
import com.lv.common.dialog.manager.ChainNode
import com.lv.common.dialog.manager.DialogChain
import com.lv.common.imp.DecimalInputTextWatcher
import com.lv.common.imp.OnDialogSureListener
import com.lv.common.utils.AnimationUtils

/**
 * 作者： ygx
 * 创建日期：2022/5/18 9:53 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * _              _           _     _   ____  _             _ _
 * / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
class InputDialog : BaseDialog, ChainNode {
    private var title: String? = null
    private var mIsShowCancle: Boolean = true
    private var mIsShowDelete: Boolean = true
    private var content: String? = null
    private var tv_title: TextView? = null
    private var etContent: EditText? = null
    private var tv_cancel: TextView? = null
    private var tv_sure: TextView? = null
    private var ivClose: ImageView? = null
    private var ivClear: ImageView? = null
    private var view_line_bottom: View? = null
    private var INPUT_STATE: DIALOG_INPUT_ENUM? = DIALOG_INPUT_ENUM.NOMAL

    constructor(mContext: Context?, isCancelable: Boolean, isBackCancelable: Boolean) : super(
        mContext,
        isCancelable,
        isBackCancelable
    ) {
    }

    constructor(
        mContext: Context?,
        title: String?,
        enum: DIALOG_INPUT_ENUM?,
        isCancelable: Boolean = true,
        isBackCancelable: Boolean = true,
        onDialogSureListener: OnDialogSureListener?,
        isShowCancle: Boolean = true,
        isShowDelete: Boolean = true
    ) : super(mContext, isCancelable, isBackCancelable) {
        this.title = title
        this.INPUT_STATE = enum
        this.mIsShowCancle = isShowCancle
        this.mIsShowDelete = isShowDelete
        this.onDialogSureListener = onDialogSureListener
    }

    constructor(
        mContext: Context?,
        isCancelable: Boolean,
        isBackCancelable: Boolean,
        gravity: Int,
        widthIsMatch: Boolean,
        heightIsMatch: Boolean
    ) : super(mContext, isCancelable, isBackCancelable, gravity, widthIsMatch, heightIsMatch) {
    }

    override fun initView() {
        tv_title = findViewById(R.id.tv_title)
        etContent = findViewById(R.id.et_content)
        ivClear = findViewById(R.id.iv_clear)
        tv_cancel = findViewById(R.id.tv_cancel)
        tv_sure = findViewById(R.id.tv_sure)
        ivClose = findViewById(R.id.iv_close)
        view_line_bottom = findViewById(R.id.view_line_bottom)
        if (!mIsShowCancle) {
            view_line_bottom?.visibility = View.GONE
            tv_cancel?.visibility = View.GONE
        }
        if (!mIsShowDelete) {
            ivClose?.visibility = View.GONE

        }
        initEvnet()
    }

    private fun initEvnet() {
        etContent?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                content = s.toString()
                if (content.isNullOrEmpty()) {
                    ivClear?.visibility = View.GONE
                } else {
                    ivClear?.visibility = View.VISIBLE
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }
        })
        tv_title?.text = title
        ivClear?.setOnClickListener {
            content = ""
            etContent?.setText("")
        }
        tv_sure?.setOnClickListener {
            if (content.isNullOrEmpty()) {
                AnimationUtils.setHintAnimation(etContent)
            } else {
                onDialogSureListener?.onSureClick(content)
                dismiss()
            }
        }
        tv_cancel?.setOnClickListener { dismiss() }
        findViewById<ImageView>(R.id.iv_close)?.setOnClickListener { dismiss() }
        when (INPUT_STATE) {
            DIALOG_INPUT_ENUM.SINGLE -> {
                etContent?.maxLines = 1
                etContent?.ellipsize = TextUtils.TruncateAt.END

                etContent?.isSingleLine = true
                etContent?.imeOptions = EditorInfo.IME_ACTION_DONE

            }

            DIALOG_INPUT_ENUM.NUMBER -> {
                etContent?.inputType = InputType.TYPE_CLASS_NUMBER
            }
            DIALOG_INPUT_ENUM.INPUT_MONEY -> {
                etContent?.addTextChangedListener(DecimalInputTextWatcher(etContent, 10, 2))
                etContent?.inputType =
                    InputType.TYPE_CLASS_NUMBER + InputType.TYPE_NUMBER_FLAG_DECIMAL
            }
            DIALOG_INPUT_ENUM.PHONE -> {
                etContent?.inputType = InputType.TYPE_CLASS_NUMBER
                etContent?.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
            }

            DIALOG_INPUT_ENUM.NUMB_CHAT -> {//数字和字母
                etContent?.transformationMethod = null
                etContent?.keyListener = object : DigitsKeyListener() {
                    override fun getInputType(): Int {
                        return InputType.TYPE_TEXT_VARIATION_PASSWORD
                    }

                    override fun getAcceptedChars(): CharArray {
                        return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray()
                    }
                }
            }
            else -> {}
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_input
    }

    private var onDialogSureListener: OnDialogSureListener? = null


    override fun getId(): Int = 0

    override fun proceed(chain: DialogChain?) {
        show()
        setOnDismissListener { dialog -> chain?.proceed() }
    }
}

enum class DIALOG_INPUT_ENUM {
    SINGLE, NUMBER, NOMAL, PHONE, NUMB_CHAT, IDENTIFICATION, INPUT_MONEY
}